var dtGridColumns = [{
    id : 'id',
    title : '编号',
    type : 'number',
    columnClass : 'text-center',
    hideType : 'xs',
    headerClass : 'dlshouwen-grid-header'
},{
    id : 'title',
    title : '广告名称',
    type : 'string',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs'
},{
    id : 'resourceType',
    title : '资源类型',
    type : 'string',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs'
},{
    id : 'typeName',
    title : '广告类型',
    type : 'string',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs'
},{
    id : 'resourceUrl',
    title : '图片',
    type : 'string',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs',
    resolution : function(value, record, column, grid, dataNo, columnNo) {
            if(value != null && value != "")
            {
                return '<img id="imgCover" style="height:40px; width:90px;" alt="" src="'+'/webside-huoyan-manage'+value+'"/>';
            
            }
            else
            {
                return '';
            }
        }
},{
    id : 'startDate',
    title : '开始日期',
    type : 'date',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs',
    resolution : function(value, record, column, grid, dataNo, columnNo) {
  	      return "<span>"+value+"</span>";
  	    }
},
{
    id : 'endDate',
    title : '截止日期',
    type : 'date',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs',
    resolution : function(value, record, column, grid, dataNo, columnNo) {
    	var thetime =value;
    	var   d=new   Date(Date.parse(thetime .replace(/-/g,"/")));
    	var   curDate=new   Date();
    	if((curDate-d)>-259200000 && (curDate-d)<=0){
    		return "<span style='color:red'>"+value+"(即将到期)</span>"; 
    	}else if((curDate-d)>0){
    		 return "<span style='color:blue'>"+value+"(已到期)</span>";
    	}else{
    		return "<span style='color:green'>"+value+"(有效)</span>";
    	}
    }
} ,
{
    id : 'status',
    title : '状态',
    type : 'string',
    columnClass : 'text-center',
    headerClass : 'dlshouwen-grid-header',
    hideType : 'xs',
    resolution : function(value, record, column, grid, dataNo, columnNo) {
    	if(value.trim()=="" || value=="已下架"){
    		return "<a href='#' style='text-decoration:none;color:red' onclick='changes(this)'>已下架</a>";
    	}else{
    		return "<a href='#' style='text-decoration:none;color:green' onclick='changes(this)'>已上架</a>";
    	}
    	
    }
},
];


//动态设置jqGrid的rowNum
var pageSize = $("#pageSize").val();
pageSize = pageSize == 0 || pageSize == "" ? sys.pageNum : pageSize;

var dtGridOption = {
    lang : 'zh-cn',
    ajaxLoad : true,
    check : true,
    checkWidth :'37px',
    extraWidth : '37px',
    loadURL : sys.rootPath + '/localad/list.html',
    columns : dtGridColumns,
    gridContainer : 'dtGridContainer',
    toolbarContainer : 'dtGridToolBarContainer',
    tools : '',
    exportFileName :'字典管理信息',
    pageSize : pageSize,
    pageSizeLimit : [10, 20, 30]
};

var grid = $.fn.dlshouwen.grid.init(dtGridOption);
$(function() {
    if(null != $("#orderByColumn").val() && '' != $("#orderByColumn").val())
    {
        grid.sortParameter.columnId = $("#orderByColumn").val();
        grid.sortParameter.sortType = $("#orderByType").val();
    }
    grid.load();
    $("#btnSearch").click(customSearch);
    
    //注册回车键事件
    document.onkeypress = function(e){
    var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
            customSearch();
        }
    };
    
});

/**
 * 自定义查询
 * 这里不传入分页信息，防止删除记录后重新计算的页码比当前页码小而导致计算异常
 */
function customSearch() {
    grid.parameters = new Object();
    grid.parameters['title'] = $("#searchKey").val();
    grid.refresh(true);
}